package speed.mybatis;

import java.sql.SQLException;

import javax.sql.DataSource;

import org.logicalcobwebs.proxool.ProxoolDataSource;

import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.name.Named;

/**
 * @author zhaohaolin(zhaohaolin2010@gmail.com)
 */
public class ProxoolPooledDataSourceProvider implements Provider<DataSource> {

	@Inject
	public ProxoolPooledDataSourceProvider(
			@Named("JDBC.driver") final String driver,
			@Named("JDBC.url") final String url,
			@Named("JDBC.username") final String username,
			@Named("JDBC.password") final String password) {
		dataSource = new ProxoolDataSource();
		dataSource.setDriver(driver);
		dataSource.setDriverUrl(url);
		dataSource.setUser(username);
		dataSource.setPassword(password);
	}

	@Inject(optional = true)
	public void setLoginTimeout(
			@Named("JDBC.loginTimeout") final int loginTimeout) {
		try {
			dataSource.setLoginTimeout(loginTimeout);
		} catch (SQLException e) {
			throw new RuntimeException((new StringBuilder())
					.append("Impossible to set login timeout '")
					.append(loginTimeout).append("' to Unpooled Data Source")
					.toString(), e);
		}
	}

	@Inject(optional = true)
	public void setMaximumActiveConnections(int maximumActiveConnections) {
		dataSource.setMaximumConnectionCount(maximumActiveConnections);
	}

	@Inject(optional = true)
	public void setMaximumCheckoutTime(int maximumCheckoutTime) {
		dataSource.setMaximumActiveTime(maximumCheckoutTime);
	}

	public void setMaximumIdleConnections(int maximumIdleConnections) {
		//
	}

	public void setPingConnectionsNotUsedFor(int pingConnectionsNotUsedFor) {
		//
	}

	public void setPingEnabled(boolean pingEnabled) {
		//
	}

	public void setPingEnabled(String pingQuery) {
		//
	}

	public void setTimeToWait(int timeToWait) {
		//
	}

	public DataSource get() {
		return dataSource;
	}

	private final ProxoolDataSource dataSource;

}
